package com.guet.careapp.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PageQueryModel {

  private int currentPage;
  private int pageSize;
  private String fromClause; // From子句
  private String whereClause = ""; // Where子句
  private String orderByClause = ""; // OrderBy子句
  private Map<String, Object> parameters = new HashMap<String, Object>(); // 参数列表

  public PageQueryModel(Class clazz, int currentPage, int pageSize,
      Map<String, Object> queryConditionMap, List<StringMap> orderConditionMap) {
    super();
    this.currentPage = currentPage;
    this.pageSize = pageSize;
    this.fromClause = "from " + clazz.getName();
    this.parameters = queryConditionMap;
    addOrderByProperty(orderConditionMap);
    addWhereCondition(queryConditionMap);
  }

  public PageQueryModel(int currentPage, int pageSize, Map<String, Object> queryConditionMap,
      List<StringMap> orderConditionMap) {
    super();
    this.currentPage = currentPage;
    this.pageSize = pageSize;
    this.parameters = queryConditionMap;
    addOrderByProperty(orderConditionMap);
    addWhereCondition(queryConditionMap);
  }

  public void setClazz(Class clazz) {
    this.fromClause = "from " + clazz.getName();
  }

  public String getListHql() {
    String hql = fromClause + whereClause + orderByClause;
    Info.print("ListHql：" + hql);
    return hql;
  }

  public String getCountHql() {
    String hql = "select COUNT(*) " + fromClause + whereClause + orderByClause;
    Info.print("CountHql：" + hql);
    return hql;
  }

  /**
   * 拼接OrderBy子句
   *
   * @param orderConditionMap true表示升序，false表示降序
   */
  public void addOrderByProperty(List<StringMap> orderConditionMap) {
    StringBuffer orderCondition = new StringBuffer();
    if (orderConditionMap.size() > 0) {
      // 方便遍历，第一个特殊（无"and")
      orderCondition.append(
          " ORDER BY " + orderConditionMap.get(0).key + " " + orderConditionMap.get(0).value);
      if (orderConditionMap.size() > 1) {
        for (int i = 1; i < orderConditionMap.size(); i++) {
          orderCondition
              .append(" , " + orderConditionMap.get(i).key + " " + orderConditionMap.get(i).value);
        }
      }
    }
    this.orderByClause = orderCondition.toString();
    Info.print("Order by Str :" + orderCondition.toString());
  }

  /**
   * 拼接Where子句
   */
  public void addWhereCondition(Map<String, Object> queryConditionMap) {
    StringBuffer queryCondition = new StringBuffer();
    List<StringMap> mapToList = Convertor.MapToList(queryConditionMap);
    // 拼接
    if (queryConditionMap.size() > 0) {
      // 方便遍历，第一个特殊（无"and")
      queryCondition.append(" WHERE " + mapToList.get(0).key + "=:" + mapToList.get(0).key);
      if (mapToList.size() > 1) {
        for (int i = 1; i < mapToList.size(); i++) {
          queryCondition.append(" AND " + mapToList.get(i).key + "=:" + mapToList.get(i).key);
        }
      }
    }
    parameters.putAll(queryConditionMap);
    this.whereClause = queryCondition.toString();
    Info.print(
        "parameterSize: " + queryConditionMap.size() + " " + "whereClause:" + this.whereClause);
  }

  public int getCurrentPage() {
    return currentPage;
  }

  public void setCurrentPage(int currentPage) {
    this.currentPage = currentPage;
  }

  public int getPageSize() {
    return pageSize;
  }

  public void setPageSize(int pageSize) {
    this.pageSize = pageSize;
  }

  public String getFromClause() {
    return fromClause;
  }

  public void setFromClause(String fromClause) {
    this.fromClause = fromClause;
  }

  public String getWhereClause() {
    return whereClause;
  }

  public void setWhereClause(String whereClause) {
    this.whereClause = whereClause;
  }

  public String getOrderByClause() {
    return orderByClause;
  }

  public void setOrderByClause(String orderByClause) {
    this.orderByClause = orderByClause;
  }

  public Map<String, Object> getParameters() {
    return parameters;
  }

  public void setParameters(Map<String, Object> parameters) {
    this.parameters = parameters;
  }

}
